//
//  GuideViewController.swift
//  SwiftProject
//
//  Created by 董駸 on 2022/7/25.
//

import UIKit

// 引导页张数
var guideCount : Int = 3
// 是否根据版本号显示引导页 默认否
let showGuideUseVersion : Bool = false
let showGuideKey : String = "showGuide"
let showGuideWithVersionKey = "showGuideWithVersion"

class GuideViewController: BaseViewController {
    let scrollView = UIScrollView()

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func initSubviews() {
        let guideV = FQGuideView()
        guideV.show()
        
        scrollView.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT)
        scrollView.contentSize = CGSize(width: SCREEN_WIDTH * CGFloat(guideCount), height: SCREEN_HEIGHT)
        scrollView.isPagingEnabled = true
        scrollView.backgroundColor = .white
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.bounces = false
        view.addSubview(scrollView)
        
        for index in 0..<guideCount {
            let imageV = UIImageView(frame: CGRect(x: SCREEN_WIDTH * CGFloat(index), y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
            imageV.contentMode = .scaleAspectFill
            imageV.clipsToBounds = true
            // 设置图片
            imageV.image = UIImage(named: "guide\(index + 1)")
            imageV.isUserInteractionEnabled = true
            scrollView.addSubview(imageV)
            
            if index == guideCount - 1 {
                // 最后一张设置按钮 可自定义
                imageV.backgroundColor = .red
                let button = QMUIButton()
//                button.setTitle("立即体验", for: .normal)
//                button.clipsToBounds = true
//                button.backgroundColor = .white
//                button.titleLabel?.font = .systemFont(ofSize: 15)
//                button.layer.cornerRadius = 20
//                button.layer.borderWidth = PixelOne
//                button.layer.borderColor = UIColor.gray.cgColor
                button.addTarget(self, action: #selector(imageTap), for: .touchUpInside)
                imageV.addSubview(button)
                
                button.snp.makeConstraints { make in
                    make.centerX.equalTo(imageV)
                    make.size.equalTo(CGSize(width: 170, height: 40)~)
                    make.bottom.equalTo(imageV).offset(PreferredValueForVisualDevice(regular: -35~, compace: -20~))
                }
            }
        }
    }

    @objc func imageTap() {
        // 转场动画可自定义
        UIView.transition(with: view, duration: 0.5, options: .transitionFlipFromLeft, animations: {
            AppDelegate.appDelegate.window?.rootViewController = QMUINavigationController(rootViewController: LoginViewController())
        }, completion: { isFinish in
            if showGuideUseVersion {
                UserDefaults.standard.setValue(AppVersion, forKey: showGuideWithVersionKey)
            } else {
                UserDefaults.standard.set(true, forKey: showGuideKey)
            }
        })
    }
    
    static func showGuideView() -> Bool {
        if !showGuideUseVersion {
            if !UserDefaults.standard.bool(forKey: showGuideKey) {
                return true
            }
        } else {
            if AppVersion != UserDefaults.standard.value(forKey: showGuideWithVersionKey) as! String {
                return true
            }
        }
        return false
    }
}
